﻿@model BenzInfo.Hockey.Grenadiers.Models.Team
@using BenzInfo.Hockey.Grenadiers.Models
@using MvcEnumDropDownList.Helpers;

@{
    ViewBag.Title = "Archive";
}

<script type="text/javascript">

    $(document).ready(function () {
        if ("@(ViewBag.CurrentUser != null)" == "True")
        {
            $('.players ul').sortable({
                items: "li:not(:last-child)",
                stop: function (event, ui) {

                    var items = [];
                    $.each($('li:not(:last-child)', this), function (i, elem) {
                        items[i] = $(elem).attr('id').split('_')[1];
                    });

                    $.post('Players/EditPosition', $.param({ items: items }, true));
                }
            });
        }

        $('.players ul').disableSelection();

        $('.players li').hover(function () {
            $('.buttons', this).show();
        }, function () {
            $('.buttons', this).hide();
        });

        $('#dialog_players').dialog({
            autoOpen: false,
            modal: true,
            draggable: false,
            resizable: false,
            width: 580,
            height: 'auto',
            buttons: [
                {
                    id: 'dialog_btn_save', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Save',
                    click: function () {
                        $('form', this).submit();
                    }
                },
                {
                    id: 'dialog_btn_cancel', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Cancel',
                    click: function () {
                        $(this).dialog('close');
                    }
                }
            ]
        });

        $('.players .buttons a').click(function () {
            var url = $(this).attr('href');
            var title = '';

            if (url.indexOf('Create') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Create';
                $('#dialog_btn_save span').html(title);
            }
            else if (url.indexOf('Edit') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Edit';
                $('#dialog_btn_save span').html('@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Save');
            }
            else if (url.indexOf('Delete') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Delete';
                $('#dialog_btn_save span').html(title);
            }

            $('#dialog_players').empty().load(url, function () {
                $('#dialog_players').dialog('option', 'title', title).dialog('open');
            });

            return false;
        });

        $('#cbo_years').val(@Model.Id);
    });

</script>

<h1>@(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Player)s @String.Format("{0} - {1}", Model.Year - 1, Model.Year)</h1>

<div id="dialog_players"></div>

@Html.DropDownList("cbo_years", (List<SelectListItem>)@ViewBag.Teams, new { @onchange = "location = '?teamId=' + this.value;" })

<br /><br />

@if (!string.IsNullOrEmpty(Model.Picture))
{
    <div class="team_picture">
        <img src="@Url.Content("~/Utils/ImageHandler.ashx?src=teams/" + Model.Picture + "&maxheight=500")" />
    </div>
}

<div class="players">
    <ul class="sortable archives">
    @if (Model.Id != 0)
    {
        foreach (var item in Model.Players.OrderBy(m => m.Position))
        {
            var picture_url = (item.Picture != null && item.Picture != "") ? item.Picture : "img_no_picture.jpg";
        <li id="item_@(item.Id)">
            
            <div class="info">
                <b>@Html.DisplayFor(modelItem => item.FullName)</b><br />
                @BenzInfo.Hockey.Grenadiers.Resources.Models.Player.PositionEnum_Center
                <span class="badge number">@Html.DisplayFor(modelItem => item.Number)</span>

                @if (ViewBag.CurrentUser != null)
                {
                    <span class="buttons">
                        @Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Edit, "Edit", new { id = item.Id, teamId = Model.Id })
                        @Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Delete, "Delete", new { id = item.Id, teamId = Model.Id })
                    </span>
                }
            </div>
        </li>
        }
    }
    
    @if (ViewBag.CurrentUser != null)
    {
        <li class="item_new">
            <span class="buttons">@Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Create, "Create", new { teamId = Model.Id })</span>
            <div class="info new">
                <b>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.AddPerson</b>
            </div>
        </li>
    }
    </ul>
    <div class="clear"></div>
</div>